package com.yms.string;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Stack;

/**
 * @version : 1.0
 * @author: yangmingsong
 * @date: 2022/9/3-09-03-17:19
 * @description: spring5-test
 */
public class Test {
    public static void main(String[] args) {
        //System.out.println(caculate("(4-2)*5#"));
        //System.out.println(piPei("abcdefgstdhjfgolge","fgstdh"));
        System.out.println("1234".substring(0,0));
    }

    public static String inte(String s){
        char[] chars = s.toCharArray();
        ArrayList<Character> characterArrayList = new ArrayList<>();
        Stack<Character> characterStack = new Stack<>();
        characterStack.push('#');
        for(int i=0;i<chars.length;i++){
            if(!isNotNum(chars[i])){
                characterArrayList.add(chars[i]);
            }
            if(isNotNum(chars[i])){
                if(!characterStack.empty()){
//                    取出栈顶元素
                    Character pop = characterStack.peek();
                    if(isp(pop)>icp(chars[i])){
//                        退栈并输出
                        characterStack.pop();
                        characterArrayList.add(pop);
                        for(int j=characterStack.size()-1;j>=0;j--){
                            Character next = characterStack.get(j);
                            if(isp(next)>icp(chars[i])){
                                //characterStack.pop();
                                characterStack.pop();
                                characterArrayList.add(next);
                            }else if(isp(next)<icp(chars[i])){
                                characterStack.push(chars[i]);
                                break;
                            }else {
                                characterStack.pop();
                                break;
                            }
                        }
                    }else if(isp(pop)<icp(chars[i])){
                        characterStack.push(chars[i]);
                    }else {
                        characterStack.pop();
                    }
                }else {
                    characterStack.push(chars[i]);
                }



            }


        }
        char[] chars1 = new char[characterArrayList.size()];
        for(int i=0;i<characterArrayList.size();i++){
            chars1[i] = characterArrayList.get(i);
        }
        return new String(chars1);
    }

    public static int isp(char c){
        int i = 0;

        switch (c){
            case '#':i=0;break;
            case  '(':i=1;break;
            case  '*': i=5;break;
            case  '/': i=5;break;
            case  '+': i=3;break;
            case  '-': i=3;break;
            case  ')': i=6;break;
        }
        return i;
    }

    public static boolean isNotNum(char c){
        return c=='#' || c=='(' || c=='*'
                || c=='/' || c=='+' || c=='-' || c==')';
    }

    public static int icp(char c){
        int i = 0;

        switch (c){
            case '#':i=0;break;
            case  '(':i=6;break;
            case  '*': i=4;break;
            case  '/': i=4;break;
            case  '+': i=2;break;
            case  '-': i=2;break;
            case  ')': i=1;break;
        }
        return i;
    }

    public static int caculate(String s){
        String inte = inte(s);
        char[] chars = inte.toCharArray();
        Stack<Character> objects = new Stack<>();

        for(int i=0;i<chars.length;i++){
            if(!isNotNum(chars[i])){
                objects.push(chars[i]);
            }else{
                int Y = objects.pop();
                int X = objects.pop();
                if(chars[i]=='+'){
                    objects.push((char) (X+Y-48));
                }else if(chars[i]=='-'){
                    objects.push((char) (X-Y+48));
                }else if(chars[i]=='*'){
                    objects.push((char) ((X-48)*(Y-48)+48));
                }else if(chars[i]=='/'){
                    objects.push((char) ((X-48)/(Y-48)+48));
                }
            }
        }

        return (int) objects.pop()-48;
    }

//    模式匹配
    public static int piPei(String s0,String s1){
        char[] chars0 = s0.toCharArray();
        char[] chars1 = s1.toCharArray();
        int i=0;
        int j=0;
        int flag = 0;
        while (i<s0.length() && j<s1.length()){
            if(chars0[i]==chars1[j]){
                i++;j++;
            }else {
                i = ++flag;
                j=0;
            }
        }
        if(j==s1.length()){
            return flag;
        }else{
            return 0;
        }
    }


    public static int[] PM(String s){
        //char[] chars = s.toCharArray();
        int[] pm = new int[s.length()];
        pm[0] = 0;

        int i=0,j=0;
        while (i<s.length()){

        }
        return pm;
    }
}


